package training;

import util.ObjectUtil;

import java.util.ArrayList;
import java.util.Scanner;

public class Training02 {
    private Scanner scanner = new Scanner(System.in);
    private ArrayList<Student> students;
    public Training02(){
        init();
        menu();
    }
    public void init(){
        students = ObjectUtil.read("student.dat");
        if(students == null){
            students = new ArrayList<>();
            ObjectUtil.write("student.dat",students);
        }
    }
    public void menu(){
        while(true){
            System.out.println("请选择：1-增加学生 2-查看学生 3-退出");
            int input = scanner.nextInt();
            if(input == 1){
                add();
            }else if(input == 2){
                show();
            }else if(input == 3){
                System.out.println("BYE");
                System.exit(0);
            }else{
                System.out.println("输入有误");
            }
        }
    }
    public void add(){
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入性别：");
        String gender = scanner.next();
        Student student = new Student(name,age, gender);
        students.add(student);
        ObjectUtil.write("student.dat",students);
        System.out.println("增加成功");
    }
    public void show(){
        for(Student student: students){
            System.out.println(student);
        }
    }
    public static void main(String[] args) {
        new Training02();
    }
}
